package com.daasuu.cat;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.text.DecimalFormat;


/**
 * Count animation text view
 */
public class CountAnimationTextView extends Text {
    /**
     * The constant Is animating
     */
    private boolean isAnimating = false;
    /**
     * The constant M count animator
     */
    private ValueAnimator mCountAnimator;
    /**
     * The constant M count animation listener
     */
    private CountAnimationListener mCountAnimationListener;

    /**
     * The constant M decimal format
     */
    private DecimalFormat mDecimalFormat;

    /**
     * * The constant DEFAULT_DURATION
     */
    private static final long DEFAULT_DURATION = 1000;

    /**
     * Count animation text view
     *
     * @param context context
     */
    public CountAnimationTextView(Context context) {
        this(context, null, null);
    }

    /**
     * Count animation text view
     *
     * @param context context
     * @param attrs   attrs
     */
    public CountAnimationTextView(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    /**
     * Count animation text view
     *
     * @param context      context
     * @param attrs        attrs
     * @param defStyleAttr def style attr
     */
    public CountAnimationTextView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setUpAnimator();
    }

    /**
     * Set up animator
     */
    private void setUpAnimator() {
        mCountAnimator = new ValueAnimator();
        mCountAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float value) {
                String text;
                if (mDecimalFormat == null) {
                    text = String.valueOf((int) value);
                } else {
                    text = mDecimalFormat.format((int) value);
                }
                CountAnimationTextView.super.setText(text);
            }
        });

        mCountAnimator.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
                isAnimating = true;

                if (mCountAnimationListener == null) {
                    return;
                }

                mCountAnimationListener.onAnimationStart((int) mCountAnimator.getStart());
            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {
                isAnimating = false;

                if (mCountAnimationListener == null) {
                    return;
                }
                mCountAnimationListener.onAnimationEnd((int) mCountAnimator.getEnd());
            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        });
        mCountAnimator.setDuration(DEFAULT_DURATION);
    }

    /**
     * On detached from window
     */
    protected void onDetachedFromWindow() {
        if (mCountAnimator != null) {
            mCountAnimator.cancel();
        }
    }

    /**
     * Count animation *
     *
     * @param fromValue from value
     * @param toValue   to value
     */
    public void countAnimation(int fromValue, int toValue) {
        if (isAnimating) {
            return;
        }

        mCountAnimator.setFloatValues(fromValue, toValue);
        mCountAnimator.start();
    }

    /**
     * Set animation duration count animation text view
     *
     * @param duration duration
     * @return the count animation text view
     */
    public CountAnimationTextView setAnimationDuration(long duration) {
        mCountAnimator.setDuration(duration);
        return this;
    }

    /**
     * Set interpolator count animation text view
     *
     * @param value value
     * @return the count animation text view
     */
    public CountAnimationTextView setInterpolator(int value) {
        mCountAnimator.setInterpolator(value);
        return this;
    }


    /**
     * Count animation listener
     */
// interface progress animationListener
    public interface CountAnimationListener {
        /**
         * On animation start *
         *
         * @param animatedValue animated value
         */
        void onAnimationStart(Object animatedValue);

        /**
         * On animation end *
         *
         * @param animatedValue animated value
         */
        void onAnimationEnd(Object animatedValue);
    }

    /**
     * Set decimal format count animation text view
     *
     * @param mDecimalFormat m decimal format
     * @return the count animation text view
     */
    public CountAnimationTextView setDecimalFormat(DecimalFormat mDecimalFormat) {
        this.mDecimalFormat = mDecimalFormat;
        return this;
    }

    /**
     * Clear decimal format
     */
    public void clearDecimalFormat() {
        this.mDecimalFormat = null;
    }

    /**
     * Set count animation listener count animation text view
     *
     * @param mCountAnimationListener m count animation listener
     * @return the count animation text view
     */
    public CountAnimationTextView setCountAnimationListener(CountAnimationListener mCountAnimationListener) {
        this.mCountAnimationListener = mCountAnimationListener;
        return this;
    }
}
